﻿using System;
using System.Data.Entity;
using System.Linq;
using IFDB.Models.Contexts;
using IFDB.Models.Tables;
using IFDB.ResourceLibrary;
using IFDB.ViewModels.Foundation;
using IFDB.ViewModels.PartialViewModels.Interfaces;
using PagedList;

namespace IFDB.ViewModels.PartialViewModels
{
    public class CompetitionFixtureListViewModel : BaseViewModel, ICompetitionMatchListViewModel, IPaginationViewModel<Match>
    {
        #region Constructor
        public CompetitionFixtureListViewModel(Competition competition, Guid seasonId, DateTime startdate, DateTime endDate, int page)
        {
            this.Competition = competition;
            this.SeasonId = seasonId;
            this.StartDate = startdate;
            this.EndDate = endDate;
            this.page = page;
        }
        #endregion

        #region Fields
        private int page;
        private IPagedList<Match> competitionMatchList;
        #endregion

        #region ICompetitionMatchListViewModel implementation
        public Competition Competition { get; set; }
        public Guid SeasonId { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        
        public IPagedList<Match> CompetitionMatchList
        {
            get
            {
                if (competitionMatchList == null)
                    using (IFDBContext db = new IFDBContext())
                        competitionMatchList = (from m in db.Matches.Include(m => m.Team1).Include(m => m.Team2)
                                                where m.CompetitionId == Competition.CompetitionId
                                                && m.MatchDate <= EndDate
                                                && m.MatchDate >= StartDate
                                                && (m.MatchStatus == MatchStatus.NotPlayed)
                                                select m)
                                                .OrderBy(m => m.MatchDate)
                                                .ToPagedList(page, 15);

                return competitionMatchList;
            }
        }
        #endregion

        #region IPaginationViewModel<Match>
        public IPagedList<Match> PagedList 
        {
            get { return CompetitionMatchList; }
        }

        public string ActionName
        {
            get { return "_FixtureList"; }
        }

        public string ControllerName
        {
            get { return "League"; }
        }

        public object FirstHtmlAttributes
        {
            get { return new { competitionId = Competition.CompetitionId, seasonId = SeasonId, startDate = StartDate, endDate = EndDate, page = 1 }; }
        }

        public object PreviousHtmlAttributes
        {
            get { return new { competitionId = Competition.CompetitionId, seasonId = SeasonId, startDate = StartDate, endDate = EndDate, page = PagedList.PageNumber - 1 }; }
        }

        public object NextHtmlAttributes
        {
            get { return new { competitionId = Competition.CompetitionId, seasonId = SeasonId, startDate = StartDate, endDate = EndDate, page = PagedList.PageNumber + 1 }; }
        }

        public object LastHtmlAttributes
        {
            get { return new { competitionId = Competition.CompetitionId, seasonId = SeasonId, startDate = StartDate, endDate = EndDate, page = PagedList.PageCount }; }
        }

        public string TargetId
        {
            get { return "FixtureList"; }
        }
        #endregion
    }
}
